package com.mock.ems.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.mock.base.util.CollectionUtil;
import com.mock.base.util.DateUtil;
import com.mock.base.util.StringUtil;

import java.util.*;

/**
 * Created by MOCK on 2017/8/25.
 */
public class ScoreDetailController extends Controller {


    public void index() {
        renderJsp("/page/score/score_list.jsp");
    }

    public void pagination() {

        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");

        String selecSQL = "select * ";

        StringBuilder exceptSelectSQL = new StringBuilder(" from user_score_record where 1 = 1 ");
        List<Object> params = new ArrayList<Object>();

        Date begin_time = DateUtil.parseToDate(getPara("begin_time"),DateUtil.FULL_PATTERN);
        Date end_time = DateUtil.parseToDate(getPara("end_time"),DateUtil.FULL_PATTERN);


        String action = getPara("action");
        if (StringUtil.isNotEmpty(action)) {
            exceptSelectSQL.append(" and action like ? ");
            params.add('%' + action.trim() + '%');
        }

        String nickName = getPara("nick_name");
        if (StringUtil.isNotEmpty(nickName)) {
            List<Record> userList = Db.find("select id from user where nick_name like ? ",'%' + nickName + '%');
            if (CollectionUtil.isNotEmpty(userList)) {
                exceptSelectSQL.append(" and user_id in (");
                userList.forEach(item0 -> {
                    exceptSelectSQL.append("?,");
                    params.add(item0.getLong("id"));
                });
                exceptSelectSQL.deleteCharAt(exceptSelectSQL.length() - 1);
                exceptSelectSQL.append(") ");
            }
        }

        if (null != begin_time && end_time == null) {
            exceptSelectSQL.append(" and create_time >= ? ");
            params.add(begin_time);
        }

        if (null == begin_time && end_time != null) {
            exceptSelectSQL.append(" and create_time <= ? ");
            params.add(end_time);
        }

        if (null != begin_time && null != end_time) {
            exceptSelectSQL.append(" and create_time >= ? and create_time <= ? ");
            params.add(begin_time);
            params.add(end_time);
        }


        exceptSelectSQL.append(" order by id desc ");
        Page<Record> recordList = null;
        if (params.size() == 0) {
            recordList = Db.paginate(pageNumber, pageSize, selecSQL, exceptSelectSQL.toString());
        }else {
            recordList = Db.paginate(pageNumber, pageSize, selecSQL, exceptSelectSQL.toString(),params.toArray());
        }

        if (CollectionUtil.isNotEmpty(recordList.getList())) {
            recordList.getList().forEach(item -> {
                Long userId = item.getLong("user_id");
                String nick_name = Db.queryStr("select nick_name from user where id = ? ",userId);
                item.set("nick_name",nick_name);
            });
        }

        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("total", recordList.getTotalRow());
        returnMap.put("rows", recordList.getList());
        renderJson(returnMap);
    }
}
